<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px"
			class="queryForms">
			<el-form-item label="产品名称" prop="deviceName">
				<el-input v-model="queryParams.deviceName" placeholder="请输入产品名称" clearable
					@keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="供方" prop="supplier">
				<el-input v-model="queryParams.supplier" placeholder="请输入供方" clearable @keyup.enter.native="handleQuery"
					maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			生成表单(没有接口,列表是错的)
			<right-toolbar menuNames="eoutsource46" @changeTableData="changeTableData" :showSearch.sync="showSearch"
				@queryTable="handleQuery" :columns.sync="columns" perm="quality:stock:eoutsource:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''"
			class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="auxiliaryConsumablesTypeList"
				@selection-change="handleSelectionChange" border>
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="送检人员" prop="inspectortName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[0].visible" />
				<el-table-column label="送检时间" prop="inspectortTime" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[1].visible" />
				<el-table-column label="供方" prop="supplier" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[2].visible" />
				<el-table-column label="送检数量" prop="inspectortNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[3].visible" />
				<el-table-column label="产品名称" prop="deviceName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[4].visible" />
				<el-table-column label="型号/图号" prop="partNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[5].visible" />
				<el-table-column label="状态" prop="inspectStatus" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[6].visible">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.inspect_status" :value="scope.row.inspectStatus" />
					</template>
				</el-table-column>
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="180">
					<template slot-scope="scope">
						<el-button v-show="scope.row.inspectStatus == '7'" size="mini" type="text" icon="el-icon-edit"
							@click="handleExam(scope.row)" v-hasPermi="['quality:stock:eoutsource:exam']">检验</el-button>
						<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleJump(scope.row)"
							v-hasPermi="['quality:stock:eoutsource:annex']">附件</el-button>
						<el-button v-show="scope.row.inspectStatus != '7'" size="mini" type="text" icon="el-icon-edit"
							@click="handleInfo(scope.row)" v-hasPermi="['quality:stock:eoutsource:info']">详情</el-button>
					</template>
				</el-table-column>
			</el-table>

			<pagination menuNames="Pagination49" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
				:limit.sync="queryParams.pageSize" @pagination="getList" />
		</div>
		<!-- 添加或修改低值易耗品类型对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" class="add-form2" :model="form" :rules="rules" label-width="130px" :disabled="disabled">
				<div class="add-mid-title">检验项目</div>
				<el-form-item label="镀层" prop="claddingMaterial">
					<el-radio v-model="form.claddingMaterial" label="0">合格</el-radio>
					<el-radio v-model="form.claddingMaterial" label="1">不合格</el-radio>
				</el-form-item>
				<el-form-item label="涂层" prop="claddingMaterial">
					<el-radio v-model="form.claddingMaterial" label="0">合格</el-radio>
					<el-radio v-model="form.claddingMaterial" label="1">不合格</el-radio>
				</el-form-item>
				<el-form-item label="外观" prop="claddingMaterial">
					<el-radio v-model="form.claddingMaterial" label="0">合格</el-radio>
					<el-radio v-model="form.claddingMaterial" label="1">不合格</el-radio>
				</el-form-item>
				<el-form-item label="规格型号" prop="specificationModel">
					<el-radio v-model="form.specificationModel" label="0">一致</el-radio>
					<el-radio v-model="form.specificationModel" label="1">不一致</el-radio>
				</el-form-item>
				<el-form-item label="数量" prop="quantity">
					<el-radio v-model="form.quantity" label="0">一致</el-radio>
					<el-radio v-model="form.quantity" label="1">不一致</el-radio>
				</el-form-item>
				<el-form-item label="合格证明" prop="qualification">
					<el-radio v-model="form.qualification" label="0">有</el-radio>
					<el-radio v-model="form.qualification" label="1">无</el-radio>
				</el-form-item>
				<el-form-item label="螺纹" prop="qualification">
					<el-radio v-model="form.qualification" label="0">合格</el-radio>
					<el-radio v-model="form.qualification" label="1">不合格</el-radio>
				</el-form-item>
				<div class="add-mid-title">检验结果</div>
				<el-form-item label="检验方式" prop="inspectionMethod">
					<el-select v-model="form.inspectionMethod" placeholder="请选择">
						<el-option v-for="dict in dict.type.inspection_method" :key="dict.value" :label="dict.label"
							:value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="检验方法" prop="testMethod">
					<el-select v-model="form.testMethod" placeholder="请选择">
						<el-option v-for="dict in dict.type.test_method" :key="dict.value" :label="dict.label"
							:value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="数量" prop="inspectionQuantity">
					<el-input-number v-model="form.inspectionQuantity" placeholder="请输入数量" :min="0" :max="99999999"
						:precision="0" />
				</el-form-item>
				<el-form-item label="检验员" prop="checkoutId">
					<el-select v-model="form.checkoutId" placeholder="请选择" @change="changeUser" filterable>
						<el-option v-for="item in userList" :key="item.userId" :label="item.nickName"
							:value="item.userId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="不合格处置情况" prop="disposalSituation" style="width: 100%">
					<el-input type="textarea" v-model="form.disposalSituation" placeholder="请输入不合格处置情况" maxlength="100"
						show-word-limit />
				</el-form-item>
				<el-form-item label="备注" prop="remark" style="width: 100%">
					<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" maxlength="100"
						show-word-limit />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm(3)">合格</el-button>
				<el-button type="primary" @click="submitForm(4)">不合格</el-button>
				<el-button type="primary" @click="submitForm(5)">让步接收</el-button>
				<el-button @click="cancel">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listInspect, getInspect, delInspect, addInspect, updateInspect } from "@/api/quality/stock/inspect"
import { mapGetters } from "vuex"
import { getUserList, getcolumn } from "@/api/selectList"
export default {
	name: "eoutsourceStock",
	dicts: ["default_status", "inspect_status", "inspection_method", "test_method"],
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 低值易耗品类型表格数据
			auxiliaryConsumablesTypeList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				supplier: undefined,
				deviceName: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				inspectionMethod: [{ required: true, message: "请选择检验方式", trigger: "blur" }],
				testMethod: [{ required: true, message: "请选择检验方法", trigger: "blur" }],
				qualifiedQuantity: [{ required: true, message: "输入合格数量", trigger: "blur" }]
			},
			// 生产日期，保质期
			producedDatearr: [],
			// 合格证编号
			qualificationNumberarr: [],
			disabled: false,
			// 用户列表
			userList: [],
			// 列信息
			columns: [
				{ key: 0, label: `送检人员`, visible: true },
				{ key: 1, label: `送检时间`, visible: true },
				{ key: 2, label: `供方`, visible: true },
				{ key: 3, label: `送检数量`, visible: true },
				{ key: 4, label: `产品名称`, visible: true },
				{ key: 5, label: `型号/图号`, visible: true },
				{ key: 6, label: `状态`, visible: true }
			]
		}
	},
	created() {
		this.getSize()
		this.getUserList()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		getSize() {
			getcolumn({ pageName: "Pagination49" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		/** 查询低值易耗品类型列表 */
		getList() {
			this.loading = true
			listInspect(this.queryParams).then(response => {
				this.auxiliaryConsumablesTypeList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 获取用户下拉选
		getUserList() {
			getUserList().then(res => {
				this.userList = res.rows
			})
		},
		//
		changeUser() {
			let cur = this.userList.find(item => {
				return item.userId == this.form.recipientId
			})
			this.form.recipientName = cur.nickName
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.disabled = false
			this.form = {
				inspectionMethod: undefined,
				testMethod: undefined,
				qualifiedQuantity: undefined,
				producedDateSel: undefined,
				producedDate: undefined,
				qualification: "0",
				qualificationNumberSel: undefined,
				qualificationNumber: undefined,
				disposalSituation: undefined,
				remark: undefined,
				packingSound: "0",
				appearanceColour: "0",
				appearanceColourText: undefined,
				assemblyTechnology: "0",
				purchaseQuantity: "0",
				certificate: "0"
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		// handleAdd() {
		//   this.reset();
		//   this.open = true;
		//   this.title = "添加低值易耗品类型";
		// },
		/** 修改按钮操作 */
		handleExam(row) {
			this.reset()
			this.title = "检验"
			getInspect(row.id).then(response => {
				this.form = response.data
				if (response.data.qualificationNumber) {
					this.form.qualificationNumberSel = response.data.qualificationNumber.split(",")
				}
				if (response.data.producedDate) {
					this.form.producedDateSel = response.data.producedDate.split(",")
				}
				this.open = true
			})
		},
		// 详情
		handleInfo(row) {
			this.reset()
			this.title = "详情"
			getInspect(row.id).then(response => {
				this.disabled = true
				this.form = response.data
				if (response.data.qualificationNumber) {
					this.form.qualificationNumberSel = response.data.qualificationNumber.split(",")
				}
				if (response.data.producedDate) {
					this.form.producedDateSel = response.data.producedDate.split(",")
				}
				this.open = true
			})
		},
		/** 提交按钮 */
		submitForm(val) {
			this.$refs["form"].validate(valid => {
				if (valid) {
					// if (this.form.id != undefined) {
					this.form.inspectStatus = val
					this.form.producedDate = this.form.producedDateSel.join(",")
					this.form.qualificationNumber = this.form.qualificationNumberSel.join(",")
					updateInspect(this.form).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.open = false
						this.getList()
					})
					// } else {
					//   addInspect(this.form).then((response) => {
					//     this.$modal.msgSuccess("新增成功");
					//     this.open = false;
					//     this.getList();
					//   });
					// }
				}
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delInspect(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => { })
		},

		// 更改状态
		// changeStauts(row) {
		//   let data = {
		//     id: row.id,
		//     state: Number(row.state),
		//   };
		//   this.$modal
		//     .confirm("确认要修改状态吗？")
		//     .then(function () {
		//       return updateInspect(data);
		//     })
		//     .then(() => {
		//       this.$modal.msgSuccess("操作成功");
		//     })
		//     .catch(() => {
		//       row.state = row.state == "0" ? "1" : "0";
		//     });
		// },
		// 跳转附件
		handleJump(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 29,
					perm: "quality:stock:eoutsource:annex"
				}
			})
		}
		/** 导出按钮操作 */
		// handleExport() {
		//   this.download(
		//     "erp/auxiliaryConsumablesType/export",
		//     {
		//       ...this.queryParams,
		//     },
		//     `auxiliaryConsumablesType_${new Date().getTime()}.xlsx`
		//   );
		// },
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 180px !important;
}

.add-mid-title {
	width: 100%;
	margin: 20px 0 10px 0;
}
</style>
